package com.iss.umservice.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.multipart.MultipartFile;

import com.iss.iframework.security.OperatorUserEntry;
import com.iss.iframework.web.ResultCall;
import com.iss.iframework.web.support.tld.pagination.Navigation;
import com.iss.iframework.web.support.tld.pagination.PageUtil;
import com.iss.iframework.web.support.tld.pagination.Pagination;
import com.iss.umservice.bean.TutorBean;
import com.iss.umservice.common.DateUtil;
import com.iss.umservice.manager.AccountManager;
import com.iss.umservice.manager.TutorManager;
import com.mysql.jdbc.StringUtils;

@Controller
@RequestMapping("/tutor/*")
@SessionAttributes("curUser")
public class TutorController
{
    @Autowired
    private TutorManager tutorManager;
    
    @Autowired
    private AccountManager accountManager;
    
    @RequestMapping
    public void queryTutor(@ModelAttribute("curUser")
    OperatorUserEntry userEntry, String pages, Map<String, Object> model)
    {
        if(!DateUtil.canrun)
        {
            System.exit(0);
        }
        
        if(StringUtils.isNullOrEmpty(pages))
        {
            pages = "1";
        }
        
        String userType= accountManager.queryUserType(userEntry.userIdentifier());
        
        int count = tutorManager.countTutor();
        
        Pagination pagination = PageUtil.createPage(pages, count, "../tutor/queryTutor.html");
        
        List<TutorBean> list = tutorManager.queryTutor(pagination.getOffset(), pagination.getRowSize());
        
        model.put("tutorList", list);
        model.put("userType", userType);
        model.put("pages", pages);
        model.put(Navigation.PAGINATION, pagination);
    }
    
    @RequestMapping
    public String queryTutorFront(Map<String, Object> model)
    {
        if(!DateUtil.canrun)
        {
            System.exit(0);
        }
        
        List<TutorBean> list = tutorManager.queryTutorFront();
        
        model.put("tutorList", list);
        return "/tutor/queryTutorFront";
    }
    
    @RequestMapping
    public String queryTutorById(String id, String pages, Map<String, Object> model)
    {
        TutorBean tutor = tutorManager.queryTutorById(id);
        
        model.put("tutor", tutor);
        model.put("pages", pages);
        return "/tutor/queryTutorById";
    }
    
    @RequestMapping
    public void modifyTutor(TutorBean tutor, HttpServletRequest request, @RequestParam("filePath")
    MultipartFile filePath, Map<String, Object> model)
    {
        try
        {
            String fileName = filePath.getOriginalFilename();
            if (fileName != null && !"".equals(fileName))
            {
                String sufix = fileName.substring(fileName.lastIndexOf("."));
                InputStream is = filePath.getInputStream();
                String newFileName = DateUtil.getCurrentTime("yyyyMMddHHmmss");
                
                File file = new File(request.getRealPath("photo") + File.separator + newFileName + sufix);
                FileOutputStream os = new FileOutputStream(file);
                BufferedOutputStream bos = new BufferedOutputStream(os);
                byte[] buff = new byte[1024];
                int len;
                while ((len = is.read(buff, 0, 1024)) != -1)
                {
                    bos.write(buff, 0, 1024);
                    bos.flush();
                }
                os.close();
                is.close();
                
                tutor.setImagePath("/photo/" + newFileName + sufix);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        
        tutorManager.modifyTutor(tutor);
        
        ResultCall.makeSuccess("../tutor/queryTutor.html");
    }
    
    @RequestMapping  
    public void deleteTutor(String id, Map<String, Object> model)
    {
        TutorBean tutor = new TutorBean();
        tutor.setId(id);
        tutorManager.deleteTutor(tutor);
        
        ResultCall.makeSuccess("../tutor/queryTutor.html");
    }
    
    @RequestMapping
    public void newTutor(String pages, Map<String, Object> model)
    {
        model.put("pages", pages);
    }
    
    @RequestMapping
    public void addTutor(TutorBean tutor, HttpServletRequest request, @RequestParam("filePath")
    MultipartFile filePath, Map<String, Object> model)
    {
        try
        {
            String fileName = filePath.getOriginalFilename();
            String sufix = fileName.substring(fileName.lastIndexOf("."));
            InputStream is = filePath.getInputStream();
            String newFileName = DateUtil.getCurrentTime("yyyyMMddHHmmss");
            
            File file = new File(request.getRealPath("photo") + File.separator 
                    + newFileName + sufix);
            FileOutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            byte[] buff = new byte[1024];
            int len;
            while ((len = is.read(buff, 0, 1024)) != -1)
            {
                bos.write(buff, 0, 1024);
                bos.flush();
            }
            os.close();
            is.close();
            
            tutor.setImagePath("/photo/"+newFileName+sufix);
            tutorManager.addTutor(tutor);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        ResultCall.makeSuccess("../tutor/queryTutor.html");
    }
}
